package com.game.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.game.majiang.config.LogManager;
import com.game.server.web.WebServer;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.PropertyConfigurator;
import com.game.majiang.logic.LogicServer;

import javax.xml.ws.Endpoint;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author 
 * 游戏服务器入口
 * 
 */
public class Main {

    public static InitServers servers;

    /**
     * 开启监听
     * 
     * @return
     */
    public static void main(String args[]) {

        LogManager.getInstance().Log("程序入口开始启动");
        launch(args);
        cmd();
    }

    /**
     * 开始服务
     *
     * @param config
     */
    public static void launch(String args[]) {
        String logUrl = System.getProperty("user.dir") + System.getProperty("file.separator") + "config/log4j.properties";
        PropertyConfigurator.configure(logUrl);
        servers = new InitServers();
        servers.Start(args);
    }

    public static void stop() {
        servers.stop();
        System.exit(0);
        LogManager.getInstance().Log("server succeed stop.");
    }

    private static void cmd() {
        for (;;) {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            String input = "";
            try {
                input = strin.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (StringUtils.isBlank(input)) continue;
            String[] cmds = input.split(" ");
            String cmd = cmds[0].toLowerCase();
            if (cmd.equals("stop")) {
                stop();
                break;
            }
            try
            {
            	LogicServer.getInstance().Cmd(cmds);
            }
            catch(Exception ex)
            {
            	ex.printStackTrace();
            	LogManager.getInstance().Log("input is no cmd...");
            } 
        }
    }
}